//
//  DHS2_Brand_ContentCell.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/7/9.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHS2_Brand_ContentCell.h"
#import "DHBrandProductCollectionCell.h"
#import "DHS2_ProductDetailController.h"

@interface DHS2_Brand_ContentCell()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout,UIWebViewDelegate>

@property (strong, nonatomic) NSArray *dataArray;

@end

@implementation DHS2_Brand_ContentCell

- (NSArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSArray array];
    }
    return _dataArray;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    UICollectionViewFlowLayout *layout =  [[UICollectionViewFlowLayout alloc] init];
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"DHBrandProductCollectionCell" bundle:nil] forCellWithReuseIdentifier:kDHBrandProductCollectionCell];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    self.webView.backgroundColor = [UIColor whiteColor];
    self.webView.delegate = self;
}

- (void)updateCellArr:(NSArray *)arr{
    self.dataArray = arr;
    [self.collectionView reloadData];
}

#pragma mark - UICollectionViewDataSource and UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    DHBrandProductCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kDHBrandProductCollectionCell forIndexPath:indexPath];
    [cell uploadCell:self.dataArray[indexPath.row]];
    cell.leftPriceLabel.adjustsFontSizeToFitWidth = YES;
    cell.rightPriceLabel.adjustsFontSizeToFitWidth = YES;
    return cell;
}

#pragma mark ---- UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return (CGSize){(kScreenWidth - 40)/3,200};
}

// 两行之间的最小间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 5;
}

// 两个cell之间的最小间距，是由API自动计算的，只有当间距小于该值时，cell会进行换行
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *model = [self.dataArray objectAtIndex:indexPath.row];
    DHS2_ProductDetailController *vc = [[DHS2_ProductDetailController alloc] initWithProduct:model];
    [self.viewController.navigationController pushViewController:vc animated:YES];
}

#pragma mark - UIWebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    CGFloat webViewHeight = webView.scrollView.contentSize.height;
    [self routerEventWithName:@"brand_info_webViewDidFinishLoad" userInfo:@{@"height":[NSNumber numberWithFloat:webViewHeight]}];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
